package com.yunxin.customui.util;

import com.yunxin.customui.bean.table.*;
import com.yunxin.customui.service.adapter.table.TableBySQLAdapter;
import com.yunxin.customui.myinterface.table.ITableAdapter;
import com.yunxin.customui.bean.table.sqltable.TableSqlConfig;
import com.yunxin.customui.myinterface.table.ITrHandler;
import com.yunxin.tools.commonbean.DataBean;
import com.yunxin.tools.commonbean.exceptions.GeneralException;
import com.yunxin.tools.commonenum.HandleEnum;
import com.yunxin.tools.commonenum.HeaderMergeEnum;
import com.yunxin.customui.bean.table.datatable.TableBusinessConfig;
import com.yunxin.customui.bean.table.datatable.TableDataTemplate;
import com.yunxin.customui.myinterface.table.ITdHandler;
import com.yunxin.tools.data.caldata.ICalDataBeanService;
import com.yunxin.tools.data.caldata.impl.TotalWithoutSepcialValue;
import com.yunxin.tools.datautil.util.DataUtil;
import com.yunxin.tools.formulautil.DecimalFormatUtil;
import com.yunxin.tools.formulautil.Operate;
import com.yunxin.tools.plantbusiness.ConfigbusinessUtil;
import com.yunxin.tools.plantbusiness.bean.AbstractConfigBusinessBean;
import lombok.Getter;
import org.springframework.context.ApplicationContext;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;


/**
 * 提供：表格使用到的一些工具
 *
 * @author LGY
 * @date 2021/08/26
 */
public class TableUtil {

    public static ApplicationContext TableUtil_ap;

    public static ConfigbusinessUtil TableUtil_configbusinessUtil;

    public static Map<Integer,String> TableUtil_idxAndUISortMap = new HashMap<>();


    /**
     * 创建表
     */
    public static TableBean createTable(TableBusinessConfig tableDataConfig){
        return new TableBean(tableDataConfig);
    }


    /**
     * 排序函数， 传入电厂idx与sort映射关系
     */
    public static Function<List<String>,List<String>> orderFun(Map<Integer,String> idxAndUISortMap){
        return (mapMapKeys -> {
                OrderId orderId = new OrderId(true);
                for (String mapMapKey : mapMapKeys) {
                    int i = Integer.parseInt(mapMapKey.substring(1, 4));
                    orderId.addId(mapMapKey,Integer.parseInt(idxAndUISortMap.get(i)));
                }
                return orderId.getIdOrder();
        });
    }

//    public static Function<List<String>,List<String>> orderFunById(){
//        return (mapMapKeys -> {
//           Collections.sort();
//        });
//    }

    public static Function<List<String>,List<String>> orderFun(){
        return orderFun(TableUtil_idxAndUISortMap);
    }

    /**
     * 内部类。排序使用
     *
     * @author LGY
     * @date 2021/08/26
     */
    static class OrderId{
        public OrderId(boolean flag) {
            this.flag = flag;
        }
        //增序  减序？
        private boolean flag;

        @Getter
        private final LinkedList<String> idOrder = new LinkedList<>();
        private final LinkedList<Integer> indexList = new LinkedList<>();
        public void addId(String id, int index){
            //先找到这个index应该在哪个位置， 得到下标，将id插入id集合  index插入index集合
            if (indexList.size() == 0){
                indexList.add(index);
                idOrder.add(id);
            }else {
                int listIndex = 0;
                for (Integer integer : indexList) {
                    if (flag) {
                        if (integer > index) {
                            break;
                        }
                    }else {
                        if (integer <= index) {
                            break;
                        }
                    }
                    listIndex++;
                }
                indexList.add(listIndex, index);
                idOrder.add(listIndex, id);
            }
        }

    }


    public static List<List<DataBean>> handleListDataBeans(TableBusinessConfig tableDataConfig, List<List<DataBean>> listDataBeans){
        //将不同的业务数据对象放入一个集合 用于按电厂分组，为了根据业务拿到处理方式
        Map<String,List<DataBean>> dataMap = new HashMap<>();
        for (List<DataBean> listDataBean : listDataBeans) {
            if (!listDataBean.isEmpty()){
                String key = listDataBean.get(0).getDataId().substring(16, 22);
                List<DataBean> list = dataMap.get(key);
                if (list == null){
                    list = new ArrayList<>();
                }
                list.addAll(listDataBean);
                dataMap.put(key,list);
            }
        }
        //将business中配置每种点号所对应的 计算方式 变成map key businessId value 处理策略
        Map<String, String> handleImpl = new HashMap<>();
        for (TableDataTemplate tableDataTemplate : tableDataConfig.getTableDataTemplates()) {
            handleImpl.put(tableDataTemplate.getBusinessId(), tableDataTemplate.getCalHandle());
        }

        List<DataBean> listList = new ArrayList<>();
        for (Map.Entry<String, List<DataBean>> entry : dataMap.entrySet()) {
            String calImpl = handleImpl.get(entry.getKey());
            ICalDataBeanService iCalDataBeanService;
//            if (calImpl !=null && calImpl.trim().length() > 0){
//                iCalDataBeanService = (ICalDataBeanService) ap.getBean(calImpl);
//            }else {
                iCalDataBeanService = new TotalWithoutSepcialValue();
            //}
            List<DataBean> calDataResult = DataUtil.calculationByGroup(entry.getValue(), iCalDataBeanService, tableDataConfig.getMergeEnums());

            listList.addAll(calDataResult);
        }
        Map<Object, List<DataBean>> collect = listList.stream().collect(Collectors.groupingBy(dataBean -> dataBean.getDataId().substring(0, 22)));
        return new ArrayList<>(collect.values());
    }


    public static TableBean tableStyleHandle(TableConfig tableConfig, TableBean tableBean){
        for (TrBean tr:tableBean.getTrBeans()) {
            for (TdBean td : tr.getTdBeans()){
                //解析数据库配置的htmlhandle 然后交给处理器处理
                String htmlHandle = td.getHtmlHandle();
                if (htmlHandle != null && htmlHandle.trim().length() > 0){
                    htmlHandle = toDBC(htmlHandle);
                    String[] split = htmlHandle.split(";");
                    for (String s : split) {
                        int i1 = s.indexOf("(");
                        String serviceName = s.substring(0, i1);
                        String[] split1 = s.substring(i1 + 1, s.length() - 1).split(",");
                        HandlerParam param = null;
                        try {
                            if (split1.length > 2) {
                                param = new HandlerParam(getHandlerCondition(split1[0]), split1[1], split1[2]);
                            } else {
                                param = new HandlerParam(getHandlerCondition(split1[0]), "", split1[1]);
                            }
                        }catch (Exception e){
                            System.out.println("e");
                        }

                        ITdHandler bean1 = TableUtil_ap.getBean(serviceName, ITdHandler.class);
                        bean1.handler(td, param);
                    }
                }
            }
        }
        return tableBean;
    }

    public static void handleTableBean(TableBusinessConfig tableDataConfig, TableBean tableBean) {
        //公式计算，添加计算出来的列到表格头中去
        try {
            formulaCol(tableDataConfig,tableBean);
        }catch (Exception e){
            e.printStackTrace();
        }
        //处理表格，  时间名称列 等
        handlerTableBean(tableDataConfig, tableDataConfig.getMergeEnums(),tableBean);
        //处理单元格htmlHandler配置,在此之前已经将要对td的处理存到了tdBean对象中
        tableStyleHandle(tableDataConfig,tableBean);

        // 如果 colsum=1，就需要进行列合计
        if(tableDataConfig.getColSum()!=0){
            TrBean trBean = new TrBean();
            ICalDataBeanService iCalDataBeanService = HandleEnum.match(tableDataConfig.getColSum());
            LinkedList<TdBean> tdBeanLinkedList = new LinkedList<>();
            TdBean sumtdBean = new TdBean("合计");
            tdBeanLinkedList.add(new TdBean());
            tdBeanLinkedList.add(sumtdBean);
            for (int i = 0; i <tableBean.getExtraColumns().size()-2 ; i++) {
                tdBeanLinkedList.add(new TdBean());
            }
            // 如果电厂合并的话，合计就显示在名称下方
            // 前面空两位

            //sumtdBean.setColspan(tableBean.getExtraColumns().size());
            calCol(tableBean.getExtraColumns().size(),tdBeanLinkedList,tableBean,iCalDataBeanService);

            trBean.setTdBeans(tdBeanLinkedList);
            tableBean.getSpecialRow().addFirst(trBean);
        }
    }


    /**
     * 公式计算 流程 主方法
     *
     * @param tableDataConfig v表配置bean
     * @param tableBean        表的bean
     */
    private static void formulaCol(TableBusinessConfig tableDataConfig, TableBean tableBean) throws Exception {
        List<TableDataTemplate> tableDataTemplates = tableDataConfig.getTableDataTemplates();
        List<Integer> calCol = new ArrayList<>();
        //拿到所有要生成的列
        for (int i = 0; i< tableDataTemplates.size(); i++) {
            String formula = tableDataTemplates.get(i).getFormula();
            if (formula!=null && formula.trim().length() > 0){
                calCol.add(i);
            }
        }
        doFormulaCol(calCol, tableDataTemplates,tableBean);

    }

    private static void doFormulaCol(List<Integer> calCol, List<TableDataTemplate> tableDataTemplates, TableBean tableBean) throws Exception {
        List<Integer> cacheFormula = new ArrayList<>();
        //拿到了所有要计算的列在vTableTemplateBeans中对应的索引,一列一列处理
        for (Integer integer : calCol) {
            TableDataTemplate tableDataTemplate = tableDataTemplates.get(integer);
            String formula = tableDataTemplate.getFormula();
            //解析formula 得到Map<策略{businessId}，List<TdBean>>
            Map<String, List<TdBean>> formulaMap = analysisFormula(formula, tableBean);
            if (formulaMap == null){
                //说明这一列的计算公式中包含着一列 也是通过结算得到的列，并且这一列还未被计算。
                cacheFormula.add(integer);
            }else {
                Map<String,List<String>> doubleMap = new HashMap<>();
                for (Map.Entry<String, List<TdBean>> entry : formulaMap.entrySet()) {
                    String formulaAndId = entry.getKey();
                    String handler = formulaAndId.split("\\{")[0];
                    List<String> doubles = new ArrayList<>();
                    if (handler.equals("pass")){
                        for (TdBean tdBean : entry.getValue()) {
                            doubles.add(tdBean.getValue());

                        }
                    }else {
                        ICalDataBeanService iCalDataBeanService = (ICalDataBeanService) TableUtil_ap.getBean(handler);
                        DataBean dataBean = colCalHandle(entry.getValue(), iCalDataBeanService);
                        doubles.add(String.valueOf(dataBean.getDataValue()) );
                    }
                    doubleMap.put(entry.getKey(),doubles);
                }
                //调用公式
                List<String> doubles = calFormula(formula, doubleMap);
                //转为 tableBean中的一列 ， 并插到tableBean中
                tableBean.addCol(tableDataTemplate, doubles, s -> s,integer);
            }
        }
        if (cacheFormula.size() > 0){
            if (cacheFormula.size() == calCol.size()){
                throw new GeneralException("公式:"+ tableDataTemplates.get(calCol.get(0)).getFormula()+" 中的businessId是否存在循环依赖");
            }
            doFormulaCol(cacheFormula, tableDataTemplates,tableBean);
        }
    }



    /**
     * 公式计算， 真正的计算步骤
     *
     * @param formula 公式
     * @param doubles Map<占位符,数据集合>
     * @return {@link List<String>}
     */
    public static List<String> calFormula(String formula, Map<String,List<String>> doubles) throws Exception {
        int maxSize = 0;
        for (Map.Entry<String, List<String>> entry : doubles.entrySet()) {
            if (maxSize < entry.getValue().size()) maxSize = entry.getValue().size();
        }
        //异常清空的信息
        List<String> result = new ArrayList<>();
        f:for (int i = 0; i < maxSize; i++) {
            String formatFormula = formula;
            for (Map.Entry<String, List<String>> entry : doubles.entrySet()) {
                List<String> value = entry.getValue();
                double dou;
                try {
                    if (value.size() == 1){
                        dou = Double.parseDouble(value.get(0));
                    }else {
                        dou = Double.parseDouble(value.get(i));
                    }
                }catch (NumberFormatException e) {
                    result.add("数据无法计算");
                    continue f;
                }
                dou = checkValue(dou);
//                if (checked!=null){
//                    result.add(checked);
//                    continue f;
//                }
                String s = String.valueOf(DecimalFormatUtil.twoFormat(dou));
                if (dou <0){
                    String s1 = DecimalFormatUtil.twoFormat(dou);
                    s = "(0"+s1+")";
                }
                formatFormula = formatFormula.replace(entry.getKey(), s);
            }
            //调用公式方法
            try {
                double resultDou = new Operate().caculate(formatFormula);
                result.add(DecimalFormatUtil.twoFormat(resultDou));
            } catch (ArithmeticException e) {
                result.add("除数为0，跳过计算");
            }
        }
        return result;
    }

    /**
     * 检查值 ， 如果不满足需求，则返回一个特殊的字符串
     */
    private static Double checkValue(Double value){


        if (value == -999){
            return  0d;
            //return "数据缺失，跳过计算";
        }
        if (value == -9999){
            return  0d;
            //return "数据不满足计算条件";
        }
        return value;
    }

    /**
     * 分析公式 ,目前如果公式中有重复的占位符则会计算出错
     *  formula: 公式
     *  tableBean  :  表格对象
     */
    private static Map<String,List<TdBean>> analysisFormula(String formula ,TableBean tableBean){
        String replace = formula.replace("(", "").replace(")", "");
        Map<String, List<TdBean>> result = new HashMap<>();
        String strs = "+-*/#";
        byte[] bytes = strs.getBytes();
        byte[] formulaBytes = replace.getBytes();
        List<Integer[]> fuhao = new ArrayList<>();
        int j = 0;
        for (int i = 0; i < formulaBytes.length; i++) {
            for (byte aByte : bytes) {
                if (formulaBytes[i] == aByte) {
                    fuhao.add(new Integer[]{j, i});
                    i++;
                    j = i;
                }
            }
        }
        for (Integer[] integers : fuhao) {
            String substring = replace.substring(integers[0],integers[1]);
            String id = substring.split("\\{")[1].replace("}","");
            Integer colByBusinessId = tableBean.getColByBusinessId(id);
            if (colByBusinessId == null){
                return null;
            }
            result.put(substring, tableBean.getCol(colByBusinessId));
        }
        return result;
    }


    public static <T> Map<String,T> analysisFormula(String formula , Function<String,T> function){
        String replace = formula.replace("(", "").replace(")", "");
        Map<String, T> result = new HashMap<>();
        String strs = "+-*/#";
        byte[] bytes = strs.getBytes();
        byte[] formulaBytes = replace.getBytes();
        List<Integer[]> fuhao = new ArrayList<>();
        int j = 0;
        for (int i = 0; i < formulaBytes.length; i++) {
            for (byte aByte : bytes) {
                if (formulaBytes[i] == aByte) {
                    fuhao.add(new Integer[]{j, i});
                    i++;
                    j = i;
                }
            }
        }
        for (Integer[] integers : fuhao) {
            String substring = replace.substring(integers[0],integers[1]);
            String id = substring.split("\\{")[1].replace("}","");
            result.put(substring, function.apply(id));
        }
        return result;
    }

    /**
     * 根据策略对列进行运算 得到一个结果值， 可能某些公式可以用这个结果进行接下来的计算
     *
     * @param tableBeans          表的bean
     * @param iCalDataBeanService 计算接口
     */
    private static DataBean colCalHandle(List<TdBean> tableBeans, ICalDataBeanService iCalDataBeanService){
        //计算
        List<DataBean> dataBeans = new ArrayList<>();
        for (TdBean tableBean : tableBeans) {

            double dataValue = Double.parseDouble(tableBean.getValue());
            if (dataValue<-900){
                continue;
            }
            DataBean dataBean = new DataBean();
            dataBean.setDataValue(dataValue);
            dataBeans.add(dataBean);
        }
        return iCalDataBeanService.statisticsData(dataBeans,null);
        //return null;
    }

    /**
     * 处理程序表的bean
     * 新增表格的额外列（时间， 序号，  名称等）
     *
     * @param tableDataConfig 表格配置（数据库）
     * @param headerEnums      id按位合计（其中有id的 begin 和end下标，根据下标截取str分组）  枚举
     * @param tableBean        表的bean
     */
    public static void handlerTableBean(TableBusinessConfig tableDataConfig, HeaderMergeEnum[] headerEnums, TableBean tableBean){
        //判断业务id是否都是机组/agc/母线  如果时，则显示机组/agc/母线名字，否则显示电厂名字
        int diff =0;
        boolean genThFlag = true;
        List<TableDataTemplate> tableDataTemplates = tableDataConfig.getTableDataTemplates();
        for (TableDataTemplate tableDataTemplate : tableDataTemplates) {
            int i = Integer.parseInt(tableDataTemplate.getBusinessId().substring(0, 2));
            if (diff == 0){
                diff = i;
            }else {
                if (!(diff == i)){
                    //genThFlag = false;
                    break;
                }
            }

        }
        LinkedList<TdBean> col = tableBean.getCol(1);
        //循环，将id传给工具，得到一个名字对象再 根据枚举数组，决定表格前面增加哪些名称列
        //if (headerEnums == null || headerEnums.length == 0){
        if (tableDataConfig.getMergeType().equals("不合并")){
            //表头
            LinkedList<TrBean> thBeans = tableBean.getThBeans();
            TrBean first = thBeans.getFirst();
            first.getTdBeans().addFirst(new TdBean("时间"));
            tableBean.getExtraColumns().addFirst("时间");
            first.getTdBeans().addFirst(new TdBean("名称"));
            tableBean.getExtraColumns().addFirst("名称");
            first.getTdBeans().addFirst(new TdBean("序号"));
            tableBean.getExtraColumns().addFirst("序号");
            //将dataId替换成对应的描述信息
            for (int i = 0; i < col.size(); i++) {
                TdBean t = col.get(i);
                String id = t.getId();
                LinkedList<TdBean> row = tableBean.getTrBeans().get(i).getTdBeans();
                if (id == null){
                    for (TdBean bean : row) {
                        if (bean.getId()!=null){
                            id= bean.getId();
                            t = bean;
                            break;
                        }
                    }
                }

                row.addFirst(new TdBean(t.getTime()));
                if (id == null){
                    row.addFirst(new TdBean("无描述"));
                }else {
                    AbstractConfigBusinessBean abstractConfigBusinessBean = TableUtil_configbusinessUtil.get(id);
                    String desc = "";
                    if (genThFlag){
                        if(!"00".equals(abstractConfigBusinessBean.getAgcUnion().getId())){
                            desc = abstractConfigBusinessBean.getAgcUnion().getDesc();
                        }else if(!"00".equals(abstractConfigBusinessBean.getBusKvType().getId())){
                            desc = abstractConfigBusinessBean.getBusKvType().getDesc();
                        }else if(!"00".equals(abstractConfigBusinessBean.getGenType().getId())){
                            desc= abstractConfigBusinessBean.getGenType().getDesc();
                        }else{
                            desc =abstractConfigBusinessBean.getPlantType().getDesc();
                        }
                    }else {
                        desc =abstractConfigBusinessBean.getPlantType().getDesc();

                    }
                    row.addFirst(new TdBean(desc));
                }
                //序号
                row.addFirst(new TdBean(String.valueOf(i+1)));
            }
        }


        else {
            //如果是合并数据，则新增的列 根据合并时传入的枚举决定
            LinkedList<Integer> index = new LinkedList<>();
            //添加表头
            for (int j = headerEnums.length-1; j >= 0; j--) {
                HeaderMergeEnum headerEnum = headerEnums[j];
                String headDesc = headerEnum.getText();
                if (headDesc.length()>0){
                    tableBean.getThBeans().get(0).getTdBeans().addFirst(new TdBean(headDesc));
                    tableBean.getExtraColumns().addFirst(headDesc);
                    index.addFirst(headerEnum.getIdx());
                }
            }
            tableBean.getThBeans().get(0).getTdBeans().addFirst(new TdBean("序号"));
            tableBean.getExtraColumns().addFirst("序号");
            for (int i = 0; i < col.size(); i++) {
                //一个枚举对应的部分  的描述 ，比如是枚举：按电厂合并，则得到电厂的描述
                TdBean tdBean = col.get(i);
                String dataId = tdBean.getId();
                if (dataId == null){
                    int row = tdBean.getRow();
                    LinkedList<TdBean> row1 = tableBean.getRow(row);
                    for (TdBean bean : row1) {
                        if (bean.getId()!=null){
                            dataId= bean.getId();
                        }
                    }
                }
                LinkedList<TdBean> tdBeans = tableBean.getTrBeans().get(i).getTdBeans();
                if (dataId == null){
                    tdBeans.addFirst(new TdBean("无描述"));
                }else {

                    AbstractConfigBusinessBean abstractConfigBusinessBean = TableUtil_configbusinessUtil.get(dataId);
                    String[] split = abstractConfigBusinessBean.toBusinessMsg().split("\\|");

                    for (int j = index.size()-1; j >=0 ; j--) {
                        //描述
                        String desc = split[index.get(j)];
                        tdBeans.addFirst(new TdBean(desc));
                    }
                    tdBeans.addFirst(new TdBean(String.valueOf(i+1)));
                }


            }

        }
    }


    /**
     * 根据数据库配置的字符串 得到处理条件枚举类型
     *
     * @param str 数据库配置的的符号字符串
     * @return {@link HandlerConditionEnum}
     */
    private static HandlerConditionEnum getHandlerCondition(String str){
        if (str.equals(">")) return HandlerConditionEnum.大于;
        if (str.equals(">=")) return HandlerConditionEnum.大于等于;
        if (str.equals("<")) return HandlerConditionEnum.小于;
        if (str.equals("<=")) return HandlerConditionEnum.小于等于;
        if (str.equals("!=")) return HandlerConditionEnum.不等于;
        if (str.equals("in>=<")) return HandlerConditionEnum.in前闭后开;
        if (str.equals("in><=")) return HandlerConditionEnum.in前开后闭;
        if (str.equals("in><")) return HandlerConditionEnum.in前开后开;
        if (str.equals("in>=<=")) return HandlerConditionEnum.in前闭后闭;
        if (str.equals("=")) return HandlerConditionEnum.等于;
        if (str.equals("+")) return HandlerConditionEnum.加数字;
        if (str.equals("++")) return HandlerConditionEnum.加字符;
        if (str.equals("-")) return HandlerConditionEnum.减;
        if (str.equals("*")) return HandlerConditionEnum.乘;
        if (str.equals("/")) return HandlerConditionEnum.除;
        throw new GeneralException("在处理表格样式过程中，找不到名为"+str+"的处理方式，请检查tableTemplate配置");
    }


    /**
     * 讲中文符号转为英文符号
     *
     * @param input 输入
     * @return {@link String}
     */
    private static String toDBC(String input)
    {
        char[] c=input.toCharArray();
        for (int i = 0; i < c.length; i++)
        {
            if (c[i]==12288)
            {
                c[i]= (char)32;
                continue;
            }
            if (c[i]>65280 && c[i]<65375)
                c[i]=(char)(c[i]-65248);
        }
        return new String(c);
    }

    /**
     * 计算合计
     * @param offset 偏移位，
     * @param linkedList
     */
    private static void calCol(int offset,LinkedList<TdBean> linkedList,TableBean tableBean,
                        ICalDataBeanService iCalDataBeanService){
        for(int i = 0;
            i < tableBean.getColOrder().size();i++){
            LinkedList<TdBean> colList = tableBean.getCol(tableBean.getColOrder().get(i));
            List<DataBean> dataBeans = new ArrayList<>();
            for (TdBean tdBean : colList) {
                try {
                    DataBean dataBean = new DataBean();
                    dataBean.setDataValue(Double.parseDouble(tdBean.getValue()));
                    dataBeans.add(dataBean);
                }catch (NumberFormatException e){
                    //System.out.println("遇到汉字，跳过这个单元格的合计");
                }

            }
            DataBean dataBean = null;
            try {
                dataBean = iCalDataBeanService.statisticsData(dataBeans,"");
            } catch (Exception e) {
                e.printStackTrace();
            }
            TdBean tdBean = new TdBean();
            String value = String.valueOf(dataBean.getDataValue());
            BigDecimal num = new BigDecimal(value);
            DecimalFormat df = new DecimalFormat("0.00");
            value = df.format(num);
            tdBean.setValue(value);
            linkedList.add(tdBean);
        }
    }


    public static void trProcess(TableBean tableBean, TableConfig tableConfig){
        LinkedList<TrBean> trBeans = tableBean.getTrBeans();
        String trHandler = tableConfig.getTrHandler();
        if (trHandler != null && !trHandler.isEmpty()){
            List<PageUtil.MethodParam> methodParams = PageUtil.analysisMethod(trHandler);
            for (PageUtil.MethodParam methodParam : methodParams) {
                ITrHandler trH = (ITrHandler)ApplicationContextUtil.getObject(methodParam.getMethodName());
                for (TrBean trBean : trBeans) {
                    trH.handle(trBean,methodParam.getParams());
                }
            }
        }
    }

    public static TableBean tableTypeConvert(TableSqlConfig tableSqlConfig,  WebTableBean webTableBean){
        ITableAdapter<WebTableBean> adapter = new TableBySQLAdapter();
        TableBean adaptation = tableStyleHandle(tableSqlConfig,adapter.adaptation(webTableBean));
        trProcess(adaptation,tableSqlConfig);
        return adaptation;
    }
}
